package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.presenter.contract.ForgetPwdContract;
import com.shd.edanpei.presenter.ForgetPwdPresenter;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by mc on 2018/3/28.
 * 重置登录密码
 */
public class ResetLoginPwdActivity extends BaseActivity<ForgetPwdPresenter>
        implements ForgetPwdContract.ForgetPwdIView {

    public static final int UPDATA_PWD_LAYOUT = 1;//修改密码
    public static final int FORGET_PWD_LAYOUT = 2;//忘记密码

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.rl_reset_oldpwd)
    View oldPwd;
    @BindView(R.id.et_forget_account)
    EditText etForgetAccount;
    @BindView(R.id.et_forget_code)
    EditText etForgetCode;
    @BindView(R.id.tv_forget_getcode)
    TextView tvGetCode;
    @BindView(R.id.et_reset_oldpwd)
    EditText etOldPwd;
    @BindView(R.id.et_forget_password1)
    EditText etForgetPassword1;
    @BindView(R.id.et_forget_password2)
    EditText etForgetPassword2;

    private int mLayout;

    @Override
    protected int setLayout() {
        return R.layout.passwrod_reset_activity;
    }

    @Override
    protected ForgetPwdPresenter bindPresenter() {
        return new ForgetPwdPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        Bundle extras = getIntent().getExtras();
        mLayout = extras.getInt(Constant.TYPE);
        switch (mLayout) {
            case UPDATA_PWD_LAYOUT:
                headline.setText(R.string.change_password);
                oldPwd.setVisibility(View.VISIBLE);
                break;
            case FORGET_PWD_LAYOUT:
                headline.setText(R.string.forget_password);
                oldPwd.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_headline_back)
    public void onViewClicked() {
        finish();

    }

    /**
     * 获取验证码
     */
    @OnClick(R.id.tv_forget_getcode)
    void getCode() {
        if (etData(etForgetAccount)) {
            mPresenter.getCode(etForgetAccount.getText().toString(),mLayout);
        }
    }

    @OnClick(R.id.tv_forget_done)
    void done() {
        if (mLayout == FORGET_PWD_LAYOUT) {
            if (etData(etForgetAccount, etForgetCode, etForgetPassword1, etForgetPassword2)) {
                mPresenter.submit(mLayout, etForgetAccount.getText().toString(),
                        etForgetCode.getText().toString(),
                        etForgetPassword1.getText().toString(),
                        etForgetPassword2.getText().toString());
            }
        } else if (mLayout == UPDATA_PWD_LAYOUT) {
            if (etData(etForgetAccount, etForgetCode, etOldPwd, etForgetPassword1, etForgetPassword2)) {
                mPresenter.submit(mLayout, etForgetAccount.getText().toString(),
                        etForgetCode.getText().toString(),
                        etOldPwd.getText().toString(),
                        etForgetPassword1.getText().toString(),
                        etForgetPassword2.getText().toString());
            }
        }

    }

    @Override
    public TextView getCodeView() {
        return tvGetCode;
    }

}
